package com.recyclerview422zfj.model;

import com.recyclerview422zfj.bean.ArticleBean;
import com.recyclerview422zfj.bean.CollectBean;
import com.recyclerview422zfj.iface.MyCollectListListener;
import com.recyclerview422zfj.iface.MyCollectListiface;
import com.recyclerview422zfj.service.MyCollectService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by zengfanjun on 2017/6/12.
 */

public class CArticleModel implements MyCollectListiface<ArticleBean>{
    private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    public CArticleModel() {
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void getResultList(String mod, int page, String sessionID, final MyCollectListListener<ArticleBean> listener) {
        MyCollectService myCollectService=retrofit.create((MyCollectService.class));
        Call<List<CollectBean<ArticleBean>>> call=myCollectService.getCArticleList(mod,page,sessionID);
        call.enqueue(new Callback<List<CollectBean<ArticleBean>>>() {
            @Override
            public void onResponse(Call<List<CollectBean<ArticleBean>>> call, Response<List<CollectBean<ArticleBean>>> response) {
                if(response.isSuccessful()&&response.body()!=null)
                {
                    listener.onResponse(response.body());
                }
                else
                {
                    listener.onFail("FAIL");
                }

            }

            @Override
            public void onFailure(Call<List<CollectBean<ArticleBean>>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
}
}